<template>
  <div id="kfjh-create">
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
      highlight-current-row
      height="70vh"
    >
      <el-table-column
        :resizable="false"
        prop="scCustomerName"
        label="客户名称"
        width="120"
        fixed
      >
      </el-table-column>
      <el-table-column :resizable="false" prop="scId" label="编号" width="50">
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="oppId"
        v-if="false"
      ></el-table-column>
      <el-table-column
        :resizable="false"
        prop="opportunity.oppTip"
        label="机会来源"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="oppDescription"
        label="机会来源描述"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scContact"
        label="联系人"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scContactNumber"
        label="联系人电话"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scPercentageOfSuccess"
        label="成功几率(%)"
        width="60"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scOutline"
        label="概要"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scCreator"
        label="创建人"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scCreationTime"
        label="创建时间"
        width="100"
      >
      </el-table-column>
      <el-table-column :resizable="false" prop="scAssignToId" v-if="false">
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scAssignTo.name"
        label="指派给"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="scAssignTime"
        label="指派时间"
        width="100"
      >
      </el-table-column>
      <el-table-column :resizable="false" prop="oppStateId" v-if="false">
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="opportunityState.oppStateTip"
        label="销售机会状态"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        fixed="right"
        label="操作"
        width="300"
      >
        <template slot-scope="scope">
          <!-- v-if="scope.row.oppStateId == 1001" -->
          <el-button
            :disabled="scope.row.oppStateId != 1002"
            size="mini"
            type="success"
            @click="(dialogToVisible = true), (formLabelTo.row = scope.row)"
            >开发成功</el-button
          >
          <!-- @click="handleSuccess(scope.$index, scope.row)"" -->
          <el-button
            :disabled="scope.row.oppStateId != 1002"
            size="mini"
            type="primary"
            @click="handleAdd(scope.$index, scope.row)"
            >制定计划</el-button
          >
          <el-button
            :disabled="scope.row.oppStateId != 1002"
            size="mini"
            type="danger"
            @click="handleStop(scope.$index, scope.row)"
            >终止开发</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="制定开发计划"
      :visible.sync="dialogFormVisible"
      width="40vw"
    >
      <el-form
        label-position="right"
        label-width="20%"
        ref="formLabelAlign"
        :model="formLabelAlign"
        :rules="rules"
        ><!-- :inline="true":自动多行处理 -->
        <el-form-item label="创建时间" prop="createDate" w>
          <el-date-picker
            style="width: 100%"
            v-model="formLabelAlign.createDate"
            type="date"
            placeholder="选择时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划内容" prop="content">
          <el-input
            type="textarea"
            v-model="formLabelAlign.content"
            placeholder="请输入计划内容"
          ></el-input>
        </el-form-item>

        <div style="padding-top: 10px"></div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('formLabelAlign')">
          创建计划
        </el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="开发成功 并 创建客户"
      :visible.sync="dialogToVisible"
      width="80vw"
    >
      <el-form
        label-position="right"
        label-width="120px"
        ref="formLabelTo"
        :model="formLabelTo"
        :rules="rulesTo"
        inline
        ><!-- :inline="true":自动多行处理 -->
        <el-form-item label="客户地区" prop="customerArea">
          <el-input
            placeholder="请输入客户地区"
            v-model="formLabelTo.customerArea"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户行业" prop="customerIndustry">
          <el-input
            placeholder="请输入客户行业"
            v-model="formLabelTo.customerIndustry"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户等级" prop="customerGrade">
          <el-select
            style="width: 100%"
            v-model="formLabelTo.customerGrade"
            placeholder="请选择客户等级"
          >
            <el-option
              v-for="item in [
                { id: 'A', tip: '重点客户' },
                { id: 'B', tip: '普通客户' },
                { id: 'C', tip: '非优先客户' },
              ]"
              :key="item.id"
              :label="item.tip"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="满意度" prop="customerSatisfaction">
          <el-rate
            style="padding-top: 10px"
            v-model="formLabelTo.customerSatisfaction"
          ></el-rate>
        </el-form-item>
        <el-form-item label="信用度" prop="customerCreditLine">
          <el-rate
            style="padding-top: 10px"
            v-model="formLabelTo.customerCreditLine"
          ></el-rate>
        </el-form-item>
        <el-form-item label="客户地址" prop="customerAddress">
          <el-input
            placeholder="请输入客户地址"
            v-model="formLabelTo.customerAddress"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户邮编" prop="customerPostcode">
          <el-input
            placeholder="请输入客户邮编"
            v-model.number="formLabelTo.customerPostcode"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="customerPhone">
          <el-input
            placeholder="请输入联系人电话"
            v-model.number="formLabelTo.customerPhone"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户传真" prop="customerFax">
          <el-input
            placeholder="请输入客户传真"
            v-model="formLabelTo.customerFax"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户网站" prop="customerUrl">
          <el-input
            placeholder="请输入客户网站"
            v-model="formLabelTo.customerUrl"
          ></el-input>
        </el-form-item>
        <el-form-item label="营业执照注册号" prop="customerBusinessLicense">
          <el-input
            placeholder="请输入营业执照注册号"
            v-model="formLabelTo.customerBusinessLicense"
          ></el-input>
        </el-form-item>
        <el-form-item label="法人" prop="customerLegalPerson">
          <el-input
            placeholder="请输入法人名称"
            v-model="formLabelTo.customerLegalPerson"
          ></el-input>
        </el-form-item>
        <el-form-item label="注册资金" prop="customerRegisteredCapital">
          <el-input
            placeholder="请输入注册资金"
            v-model.number="formLabelTo.customerRegisteredCapital"
          ></el-input>
        </el-form-item>
        <el-form-item label="年营业额" prop="customerAnnualSalesVolume">
          <el-input
            placeholder="请输入年营业额"
            v-model.number="formLabelTo.customerAnnualSalesVolume"
          ></el-input>
        </el-form-item>
        <el-form-item label="开户银行" prop="customerDepositBank">
          <el-input
            placeholder="请输入开户银行"
            v-model="formLabelTo.customerDepositBank"
          ></el-input>
        </el-form-item>
        <el-form-item label="地税登录号" prop="customerLandTaxId">
          <el-input
            placeholder="请输入地税登录号"
            v-model="formLabelTo.customerLandTaxId"
          ></el-input>
        </el-form-item>
        <el-form-item label="国税登记号" prop="customerNationalTax">
          <el-input
            placeholder="请输入国税登记号"
            v-model="formLabelTo.customerNationalTax"
          ></el-input>
        </el-form-item>
        <el-form-item label="银行账号" prop="customerBankAccount">
          <el-input
            style="width: 20vw"
            placeholder="请输入银行账号"
            v-model="formLabelTo.customerBankAccount"
          ></el-input>
        </el-form-item>

        <div style="padding-top: 10px"></div>
        <el-form-item>
          <el-button
            type="primary"
            :round="true"
            @click="submitFormTo('formLabelTo')"
            style="width: 20vw"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitFormTo('formLabelTo')">
          立即创建
        </el-button> -->
        <el-button @click="dialogToVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    let checkNum1 = (rule, value, callback) => {
      let err = new Error("请输入正确的电话号码!");
      if (!value) {
        return callback(err);
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(err);
        } else {
          if ((value + "").length != 11) {
            callback(err);
          } else {
            callback();
          }
        }
      }, 20);
    };
    let checkNum2 = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入注册资金!"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("注册资金必须为数字值"));
        } else {
          if (value < 10000) {
            callback(new Error("注册资金必须大于10000"));
          } else {
            callback();
          }
        }
      }, 20);
    };
    let checkNum3 = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入年营业额!"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("年营业额必须为数字值"));
        } else {
          if (value < 100) {
            callback(new Error("年营业额必须大于100"));
          } else {
            callback();
          }
        }
      }, 20);
    };
    return {
      loading: true,
      opp_options: null,
      opp_state_options: null,
      tableData: null,
      dialogFormVisible: false,
      dialogToVisible: false,
      delVisible: false,
      formLabelAlign: {
        createDate: null, //创建时间
        content: null, //计划内容
      },
      rules: {
        createDate: [
          { required: true, message: "请选择创建时间!", trigger: "change" },
        ],
        content: [
          { required: true, message: "请输入计划内容!", trigger: "change" },
        ],
      },
      formLabelTo: {
        row: null,
        customerArea: null, // 客户地区
        customerIndustry: null, // 客户行业
        customerGrade: null, // 客户等级
        customerSatisfaction: 3, // 满意度
        customerCreditLine: 3, // 信用度
        customerAddress: null, // 客户地址
        customerPostcode: null, // 客户邮编
        customerPhone: null, // 联系电话
        customerFax: null, // 客户传真
        customerUrl: null, // 客户网站
        customerBusinessLicense: null, // 营业执照注册号
        customerLegalPerson: null, // 法人
        customerRegisteredCapital: null, // 注册资金
        customerAnnualSalesVolume: null, //年营业额
        customerDepositBank: null, // 开户银行
        customerLandTaxId: null, // 地税登录号
        customerNationalTax: null, // 国税登记号
        customerBankAccount: null, // 银行账号
      },
      rulesTo: {
        customerArea1: [{ required: true, message: "请输入客户名称!" }],
        opp_id: [{ required: true, message: "请选择机会来源!" }],
        opp_description: [{ required: true, message: "请输入机会来源描述!" }],
        sc_contact: [{ required: true, message: "请输入联系人!" }],
        sc_contact_number: [
          { required: true, message: "电话号码不能为空!" },
          { type: "number", message: "电话号码必须为数字值" },
          { validator: checkNum1 },
        ],
        sc_outline: [{ required: true, message: "请输入概要!" }],

        customerArea: [{ required: true, message: "请输入客户地区!" }],
        customerIndustry: [{ required: true, message: "请输入客户行业!" }],
        customerGrade: [{ required: true, message: "请输入客户等级!" }],
        customerSatisfaction: [{ required: true, message: "请输入满意度!" }],
        customerCreditLine: [{ required: true, message: "请输入信用度!" }],
        customerAddress: [{ required: true, message: "请输入客户地址!" }],
        customerPostcode: [{ required: true, message: "请输入客户邮编!" }],
        customerPhone: [
          { required: true, message: "请输入联系电话!" },
          { validator: checkNum1 },
        ],
        customerFax: [{ required: true, message: "请输入客户传真!" }],
        customerUrl: [{ required: true, message: "请输入客户网站!" }],
        customerBusinessLicense: [
          { required: true, message: "请输入营业执照注册号!" },
        ],
        customerLegalPerson: [{ required: true, message: "请输入法人!" }],
        customerRegisteredCapital: [
          { required: true, message: "请输入注册资金!" },
          { validator: checkNum2 },
        ],
        customerAnnualSalesVolume: [
          { required: true, message: "请输入年营业额!" },
          { validator: checkNum3 },
        ],
        customerDepositBank: [{ required: true, message: "请输入开户银行!" }],
        customerLandTaxId: [{ required: true, message: "请输入地税登录号!" }],
        customerNationalTax: [{ required: true, message: "请输入国税登记号!" }],
        customerBankAccount: [{ required: true, message: "请输入银行账号!" }],
      },
    };
  },
  created: function () {
    this.$axios({
      method: "get",
      crossdomain: true,
      url: "/dev-plan/chance",
    }).then((response) => {
      //   console.log(response.data.data); //请求成功返回的数据 列表信息
      this.tableData = response.data.data[0];
      this.tableData.forEach((e) => {
        e.scCreationTime = e.scCreationTime.substr(0, 10);
        e.scAssignTime = e.scAssignTime.substr(0, 10);
      });
      this.loading = false;
    });
    this.$axios({
      method: "post",
      crossdomain: true,
      url: "/opportunity/oop",
    }).then((response) => {
      // console.log(response.data.data[0]); //请求成功返回的数据 opp信息
      this.opp_options = response.data.data[0];
    });
  },
  methods: {
    handleSuccess(index, row) {
      this.formLabelAlign = JSON.parse(JSON.stringify(row));
      this.formLabelAlign.row = row; // 添加原型数据方便修改
      // console.log(this.formLabelAlign); //查看转换后
      // console.log("edit", index, this.formLabelAlign);
      this.$axios({
        method: "put",
        crossdomain: true,
        url: "/dev-plan/succeed",
        params: {
          scId: this.formLabelAlign.scId,
        },
      }).then((response) => {
        if (response.data.type == "OK") {
          // this.formLabelAlign.row = Object.assign({}, this.formLabelAlign.row, params);
          // console.log(this.formLabelAlign.row);
          this.$notify({
            title: "开发完成啦",
            message: "下班了下班了",
            type: "success",
            duration: "1000", // 1000ms后关闭
            showClose: false,
          });
          //刷新列表
          this.$axios({
            method: "get",
            crossdomain: true,
            url: "/dev-plan/chance",
          }).then((response) => {
            //   console.log(response.data.data); //请求成功返回的数据 列表信息
            this.tableData = response.data.data[0];
            this.tableData.forEach((e) => {
              e.scCreationTime = e.scCreationTime.substr(0, 10);
              e.scAssignTime = e.scAssignTime.substr(0, 10);
            });
            this.loading = false;
          });
        } else {
          this.$notify({
            title: "失败惹",
            message: response.data.msg,
            type: "error",
            duration: "4000", // 1000ms后关闭
            showClose: false,
          });
        }
      });
    },
    handleAdd(index, row) {
      this.formLabelAlign = JSON.parse(JSON.stringify(row));
      this.formLabelAlign.row = row; // 添加原型数据方便修改
      // console.log(this.formLabelAlign); //查看转换后
      this.opp_state_options = [this.formLabelAlign.opportunityState];
      this.dialogFormVisible = true;
      // console.log("edit", index, row);
    },
    handleStop(index, row) {
      this.$confirm(
        `此操作会将<${row.scCustomerName}>条目更改为<开发失败>状态, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.formLabelAlign = JSON.parse(JSON.stringify(row));
        this.formLabelAlign.row = row; // 添加原型数据方便修改
        // console.log(this.formLabelAlign); //查看转换后
        // console.log("edit", index, this.formLabelAlign);
        this.$axios({
          method: "put",
          crossdomain: true,
          url: "/dev-plan/stop",
          params: {
            scId: this.formLabelAlign.scId,
          },
        }).then((response) => {
          if (response.data.type == "OK") {
            // this.formLabelAlign.row = Object.assign({}, this.formLabelAlign.row, params);
            // console.log(this.formLabelAlign.row);
            this.$notify({
              title: "已终止开发辣",
              type: "success",
              duration: "1000", // 1000ms后关闭
              showClose: false,
            });
            //刷新列表
            this.$axios({
              method: "get",
              crossdomain: true,
              url: "/dev-plan/chance",
            }).then((response) => {
              //   console.log(response.data.data); //请求成功返回的数据 列表信息
              this.tableData = response.data.data[0];
              this.tableData.forEach((e) => {
                e.scCreationTime = e.scCreationTime.substr(0, 10);
                e.scAssignTime = e.scAssignTime.substr(0, 10);
              });
              this.loading = false;
            });
          } else {
            this.$notify({
              title: "失败惹",
              message: response.data.msg,
              type: "error",
              duration: "4000", // 1000ms后关闭
              showClose: false,
            });
          }
        });
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        // console.log(this.formLabelAlign);
        if (valid) {
          this.$axios({
            method: "post",
            crossdomain: true,
            url: "/dev-plan/plan/" + this.formLabelAlign.row.scId,
            data: this.$qs.stringify({
              //这里是发送给后台的数据
              dpDate: this.formLabelAlign.createDate, //创建时间
              dpContent: this.formLabelAlign.content, //计划内容
            }),
          }).then((response) => {
            // console.log(response); //请求成功返回的数据
            if (response.data.type == "OK") {
              // this.formLabelAlign.row = Object.assign({}, this.formLabelAlign.row, params);
              // console.log(this.formLabelAlign.row);
              this.$notify({
                title: "计划创建成功啦",
                type: "success",
                duration: "1000", // 1000ms后关闭
                showClose: false,
              });
              // this.$router.replace({ path: "/" });

              this.dialogFormVisible = false;
            } else {
              this.$notify({
                title: "修改失败惹",
                message: response.data.msg,
                type: "error",
                duration: "4000", // 1000ms后关闭
                showClose: false,
              });
            }
          });
        } else {
          this.$notify({
            title: "错了哦",
            message: "还有选项没有填对呢",
            type: "error",
            duration: "4000", // 1000ms后关闭
            showClose: false,
          });
          return false;
        }
      });
    },
    submitFormTo(formName) {
      // console.log("submitTo",this.formLabelTo);
      this.$refs[formName].validate((valid) => {
        console.log(this.formLabelTo);
        if (valid) {
          this.$axios({
            method: "post",
            crossdomain: true,
            url: "/customer/add/" + this.formLabelTo.row.scId,
            data: this.$qs.stringify(this.formLabelTo),
          }).then((response) => {
            // console.log(response); //请求成功返回的数据
            if (response.data.type == "OK") {
              this.$notify({
                title: "客户添加成功啦",
                type: "success",
                duration: "1000", // 1000ms后关闭
                showClose: false,
              });
              this.handleSuccess(0, this.formLabelTo.row); // 更改状态
              //   this.$router.replace({ path: "/" });
            } else {
              this.$notify({
                title: "失败惹",
                message: response.data.msg,
                type: "error",
                duration: "4000", // 1000ms后关闭
                showClose: false,
              });
            }
          });
        } else {
          this.$notify({
            title: "错了哦",
            message: "还有选项没有填对呢",
            type: "error",
            duration: "4000", // 1000ms后关闭
            showClose: false,
          });
          return false;
        }
      });
    },
  },
};
</script>
